#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This program is free software; you can redistribute it and/or modify  
# it under the terms of the GNU General Public License as published by  
# the Free Software Foundation; either version 2 of the License, or     
# (at your option) any later version.                                   
#                                                                         
# A copy of the license can be found in the license.txt file supplied   
# with this software or at: http://www.gnu.org/copyleft/gpl.html       
#

from celtuce.response import Response,RC_INTERNAL_ERROR

OP_INSERT = 0
OP_DELETE = 9

class   Storer(object) :
    def __init__(self) :
        self._tables = {}
        ## __init__()

    def __del__(self) :
        self.shutdown()
        ## __del__()

    def startup(self, path_list) :
        pass
        ## startup()

    def shutdown(self) :
        self._tables = {}
        ## shutdown()

    
    ####-----------------------------------------------------------####

    def insert(self, table, key, value, timestamp_ms) :
        response = Response()

        if table not in self._tables :
            self._tables[ table ] = {}

        _table = self._tables[ table ]
        _table[ key ] = (key, OP_INSERT, timestamp_ms, value)
        
        return response
        ## insert()

    def insertunique(self, table, key, value, timestamp_ms) :
        response = Response()

        if table not in self._tables :
            self._tables[ table ] = {}

        _table = self._tables[ table ]
        _table[ key ] = (key, OP_INSERT, timestamp_ms, value)
        
        return response
        ## insertunique()

    def erase(self, table, key, timestamp_ms) :
        response = Response()

        if table in self._tables :
            _table = self._tables[ table ]
            if key in _table :
                del _table[ key ]

        return response
        ## erase()

    def find(self, table, key, timestamp_ms=0) :
        response = Response()

        if table in self._tables :
            _table = self._tables[ table ]
            if key in _table :
                response.body = _table[ key ]

        if not response.body :
            response.body = (key, 0, 0, None)

        return response
        ## find()

    def findall(self, table, key, timestamp_ms=0) :
        raise NotImplementedError
        ## findall()

    ####-----------------------------------------------------------####

    def createTablet(self, table, key_begin, key_end) :
        response = Response()

        if table not in self._tables :
            self._tables[ table ] = {}

        return response
        ## createTablet()

    def dropTablet(self, table, key_begin, key_end) :
        response = Response()
        if table in self._tables :
            del self._tables[ table ]

        return response
        ## dropTablet()

    ####-----------------------------------------------------------####

    def getAvailableSpace(self) :
        return 1024 * 1024 * 1024
        ## getAvailableSpace()


    def setCellVolume(self, cell_volume) :
        pass
        ## setCellVolume()

    ## class Storer

